package com.jf.concurrent.lock;

import static java.lang.System.out;

/**
 * wait 和 notify 有两个前提
 * 1、必须使用同意把锁
 * 2、notify必须在wait之后
 *
 * @author Junfeng
 */
public class ObjectWaitNotify {

    public static void main(String[] arg) {

        Object lockObj = new Object();

        new Thread(() -> {
            synchronized (lockObj) {
                try {
                    out.println(Thread.currentThread().getName() + "  进入等待");
                    lockObj.wait();
                    out.println(Thread.currentThread().getName() + "  已被唤醒");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "AA").start();

        new Thread(() -> {
            synchronized (lockObj) {
                lockObj.notify();
                out.println(Thread.currentThread().getName() + "  唤醒 AA");
            }
        }, "BB").start();

    }
}
